<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户简称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入客户简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合作媒体" prop="signatory">
        <el-select v-model="queryParams.signatory" placeholder="请选择合作媒体" clearable>
          <el-option
            v-for="dict in dict.type.medium"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-finished"-->
<!--          size="mini"-->
<!--          @click="allApprove"-->
<!--        >全部审批通过</el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="policyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户简称" align="center" prop="nickName" />
      <el-table-column label="合作媒体" align="center" prop="signatory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.medium" :value="scope.row.signatory"/>
        </template>
      </el-table-column>
      <el-table-column label="政策类别" align="center" prop="policyType" />
      <el-table-column label="帐显返点%" align="center" prop="accountPoint" />
      <el-table-column label="私返返点%" align="center" prop="privatePoint" />
      <el-table-column label="总返点%" align="center" prop="totalPoint" />
      <el-table-column label="运营人员" align="center" prop="operateUserName" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 2 ? '' : 'success'">
            <dict-tag :options="dict.type.policy_status" :value="scope.row.status"/>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="approve(scope.row)"
            v-hasPermi="['baseMain:policy:edit']"
          >审批</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="reject(scope.row)"
            v-hasPermi="['baseMain:policy:edit']"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listPolicy, getPolicy, delPolicy, addPolicy, updatePolicy, approve, reject} from "@/api/baseMain/policy";
import { listPolicyHis} from "@/api/baseMain/policyHis";
export default {
  name: "Policy",
  dicts: ['medium','policy_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户政策表格数据
      policyList: [],
      customList: [],
      operateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        signatory: undefined,
        policyType: undefined,
        accountPoint: undefined,
        privatePoint: undefined,
        totalPoint: undefined,
        operateUser: undefined,
        operateUserName: undefined
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();

  },
  methods: {
    reject(row){
      this.$modal.confirm('是否驳回政策类别为"' + row.policyType + '"的数据项？').then(() => {
        this.loading = true;
        reject(row.policyId).then( response =>{
          this.getList();
        })
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("审核通过");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    approve(row){
      this.$modal.confirm('是否审核政策类别为"' + row.policyType + '"的数据项？').then(() => {
        this.loading = true;
        approve(row.policyId).then( response =>{
          this.getList();
        })
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("审核通过");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });

    },
    /** 查询客户政策列表 */
    getList() {
      this.queryParams.status = '2'
      this.loading = true;
      listPolicyHis(this.queryParams).then(response => {
        this.policyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nickName: undefined,
        signatory: undefined,
        policyType: undefined,
        remark: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        accountPoint: undefined,
        privatePoint: undefined,
        totalPoint: undefined,
        operateUser: undefined,
        operateUserName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户政策";
      this.getCustomNames()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getPolicy(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改客户政策";
        this.getCustomNames()
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePolicy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addPolicy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户政策编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delPolicy(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('baseMain/policy/export', {
        ...this.queryParams
      }, `policy_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
